var app = require('app')
var BrowserWindow = require('browser-window')
var path = require('path')
var ipc = require('ipc')

require("./index.js")
var frame = process.platform === 'win32'

app.on('ready', function () {
    win = new BrowserWindow({
        title: 'download',
        width: 860,
        height: 700,
        frame: frame,
        show: false,
        transparent: true
    });

    win.loadUrl('file://' + path.join(__dirname, '/public/index.html#' + JSON.stringify(process.argv.slice(2))))
    win.show();
    //win.openDevTools();

    ipc.on('close', function () {
        app.quit()
    })

    ipc.on('open-file-dialog', function () {
        var files = dialog.showOpenDialog({ properties: [ 'openFile', 'multiSelections' ]})
        if (files) win.send('add-to-playlist', files)
    })

    ipc.on('open-url-in-external', function (event, url) {
        shell.openExternal(url)
    })

    ipc.on('focus', function () {
        win.focus()
    })

    ipc.on('minimize', function () {
        win.minimize()
    })

    ipc.on('maximize', function () {
        win.maximize()
    })

    ipc.on('resize', function (e, message) {
        if (win.isMaximized()) return
        var wid = win.getSize()[0]
        var hei = (wid / message.ratio) | 0
        win.setSize(wid, hei)
    })

    ipc.on('enter-full-screen', function () {
        win.setFullScreen(true)
    })

    ipc.on('exit-full-screen', function () {
        win.setFullScreen(false)
        win.show()
    })

    ipc.on('ready', function () {
        ready = true
        if (link) win.send('add-to-playlist', [].concat(link))
        win.show()
    })



});